package com.lamatek.tags.google.beans;

import java.io.Serializable;

/**
 * GeocoderBean
 * @author Tom Cole
 * @version 0.40
 * 
 * Default address to lon/lat geocoder. It parses the current information
 * and passes responsibility to either USAddressGeocoder or IntlAddressGeocoder.
 * 
 * This class does not perform any actual geocoding itself, rather it
 * passes to the appropriate geocoder based on country.
 */
public class GeocoderBean implements Geocoder, Serializable {

    private String address = null;
    private String city = null;
    private String state = null;
    private String zip = null;
    private String country = null;
    Geocoder coder = null;
    
    /**
     * If developers wish to use other geocoders other than the USAddressGeocoder
     * and IntlAddressGeocoder, they should override this method to call
     * the geocoder of their choice.
     */
    public boolean geocode() {
        if (country == null || country.equalsIgnoreCase("US") || country.equalsIgnoreCase("USA")) {
            coder = new USAddressGeocoder();
        }
        else {
            coder = new IntlAddressGeocoder();
        }
        if (address != null)
            coder.setAddress(address);
        if (city != null)
            coder.setCity(city);
        if (state != null)
            coder.setState(state);
        if (zip != null)
            coder.setZip(zip);
        if (country != null)
            coder.setCountry(country);
        return coder.geocode();
    }
    
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
    public String getZip() {
        return zip;
    }
    public void setZip(String zip) {
        this.zip = zip;
    }
    public double getLatitude() {
        if (coder != null)
            return coder.getLatitude();
        else 
            return 0.00d;
    }
    public double getLongitude() {
        if (coder != null)
            return coder.getLongitude();
        else
            return 0.00d;
    }
    public String getPrecision() {
        if (coder != null)
            return coder.getPrecision();
        else
            return null;
    }
    public String getWarning() {
        if (coder != null)
            return coder.getWarning();
        else
            return null;
    }
}
